<?php
namespace Model;
use Think\Model,    Model\AuthModel;
    class RoleModel extends Model {
        
        protected  $_validate = array(
            array('role_name','require','角色名不能为空'),            
            array('role_name','1,10','角色名过长',0,'length'),            
        );
        
        function saveAuth($role_id,$auth_ids){
            //将权限id数组转换成字符串
            $auth_ids = implode(',', $auth_ids);
            
            //获得权限的‘控制器-操作方法’
            $atmd  = new AuthModel();
            $auths = $atmd -> select($auth_ids);
            $ac = "";
            foreach ($auths as $k => $v){
                if(!empty($v['auth_a'])&&!empty($v['auth_c'])){
                    $ac .= $v['auth_c']."-".$v[auth_a].",";
                }               
            }
            $ac = rtrim($ac,',');
            
            //更新角色权限
            $arr = array(
                'role_id' => $role_id,
                'role_auth_ids' => $auth_ids,
                'role_auth_ac' => $ac
            );
            return $this -> save($arr);
        }
}